/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.motoo.sample;

import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.google.code.motoo.NioServer;
import com.google.code.motoo.codec.CodecFilter;
import com.google.code.motoo.codec.textline.TextLineCodecFactory;
import com.google.code.motoo.core.Reader;
import com.google.code.motoo.core.TaskExecutor;
import com.google.code.motoo.core.WriteExecutor;
import com.google.code.motoo.management.ExecutorMBean;
import com.google.code.motoo.management.ReaderMBean;


public class JmxEchoServer {

	/**
	 * @param args
	 * @throws NullPointerException 
	 * @throws MalformedObjectNameException 
	 */
	public static void main(String[] args) throws Exception {
		
		MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
		NioServer server = new NioServer();
		server.getAcceptor().setBacklog(500);
		
		Reader reader = server.getReader();
		ReaderMBean readerBean = new ReaderMBean(reader);
		ObjectName readerName = new ObjectName( reader.getClass().getPackage().getName() + ":type=reader,name=" + reader.getClass().getSimpleName());
		mBeanServer.registerMBean(readerBean, readerName);
		
		TaskExecutor taskExecutor = server.getTaskExecutor();
		ExecutorMBean taskMBean = new ExecutorMBean(taskExecutor);
		ObjectName taskName = new ObjectName( taskExecutor.getClass().getPackage().getName() + ":type=task,name=" + taskExecutor.getClass().getSimpleName());
		mBeanServer.registerMBean(taskMBean, taskName);
		
		WriteExecutor writeExecutor = server.getWriteExecutor();
		ExecutorMBean writeMBean = new ExecutorMBean(writeExecutor);
		ObjectName writeName = new ObjectName( writeExecutor.getClass().getPackage().getName() + ":type=write,name=" + writeExecutor.getClass().getSimpleName());
		mBeanServer.registerMBean(writeMBean, writeName);
		
		server.setIoHandle(new EchoIoHandler());
		server.getIoFilterChain().addLast("codec", new CodecFilter(new TextLineCodecFactory("GBK")));
		server.bind(new InetSocketAddress(9000));
	}

}
